package com.xinongbest.oa.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author lemonwan
 * mybatis整合druid连接池技术；由于每种连接池技术执行的流程不一样，需要单独处理各自的逻辑
 * 引入指定的数据库连接池技术，都需要继承UnpooledDataSourceFactory，并处理对应的逻辑
 */
public class DruidDataSourceFactory extends UnpooledDataSourceFactory {

    public DruidDataSourceFactory() {
        this.dataSource = new DruidDataSource();
    }

    /**
     * druid连接池需要在获取数据源的方法中执行独有的初始化逻辑
     *
     * @return {@link DataSource}
     */
    @Override
    public DataSource getDataSource() {
        try {
            ((DruidDataSource) this.dataSource).init();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return this.dataSource;
    }
}
